package com.settlement.system.controller;

import com.settlement.system.common.constant.Number;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.service.IBaseService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import com.settlement.system.model.entity.common.TblAkcAdditionalItemDetail;
import jakarta.annotation.Resource;
import com.settlement.system.service.ITblAkcAdditionalItemDetailService;

import java.util.*;

/**
 * 爱库存-补款明细 前端控制器
 * @author Ximi
 * @since 2020-07-06
 */

@Api(tags = "爱库存-补款明细",description = "爱库存-补款明细")
@RestController
@RequestMapping("/akc/additional_item/detail")
public class TblAkcAdditionalItemDetailController extends MergeApiExcelController<TblAkcAdditionalItemDetail, Map<String,Object>> {

    @Resource
    private ITblAkcAdditionalItemDetailService tblAkcAdditionalItemDetailService;

    @Override
    public IBaseService init() {
        return tblAkcAdditionalItemDetailService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblAkcAdditionalItemDetail.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"shopNo","originId");
        super.setParamsModel(model);
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        result.put("foot",tblAkcAdditionalItemDetailService.getFoot(model));
    }

    @Override
    public String verifyImportObject(TblAkcAdditionalItemDetail tblAkcAdditionalItemDetail, AnalysisContext analysisContext) {
        if(StringUtils.isEmpty(tblAkcAdditionalItemDetail.getExchangeOrderNo())){
            return "交易订单号不能为空";
        }

        if(tblAkcAdditionalItemDetail.getExtractMoneyDate() == null){
            return "提现日期不能为空";
        }

        return null;
    }

    @Override
    protected void saveData(List<TblAkcAdditionalItemDetail> datas) throws ServiceException {
        message.set(null);
        if(needCheck.get()){
            int count = 0;
            List<TblAkcAdditionalItemDetail> temp = new ArrayList<>();
            for(int j=0;j<datas.size();j+= Number.FIVE * Number.HUNDRED){
                temp = datas.subList(j,j+Number.FIVE * Number.HUNDRED > datas.size() ? datas.size() : j+Number.FIVE * Number.HUNDRED);
                int i = tblAkcAdditionalItemDetailService.selectCountExistExtractMoneyDateByExchangeOrderNo(temp);
                count+=i;
            }
            if(count>0){
                message.set("文件中已有"+count+"条数据已维护提现日期，请问是否覆盖？");
            }
        }
        if(StringUtils.isEmpty(message.get())){
            tblAkcAdditionalItemDetailService.batchUpdateExtractMoneyDate(datas);
        }
    }
}

